package com.burgeon.backend.SHOPBACKEND.rest.marketing;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.marketing.MarketingVO;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 获取智能营销活动记录/数据表
 */
public class QueryMarketingVOData extends RESTBase {
    private final static String RANGE = "50";//分页数量
    private String ip;
    private String account;
    private String password;

    public QueryMarketingVOData(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 接收REP返回的字符串并进行数据处理
     *
     * @return
     */
    public ArrayList<MarketingVO> getData(int start) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(start), url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");


        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<MarketingVO> data = new ArrayList<MarketingVO>();
        for (int i = 0; i < jsonArray.length(); i++){
            JSONArray arr = jsonArray.getJSONArray(i);
            MarketingVO marketingVO = new MarketingVO();
            marketingVO.setId(arr.getString(0));
            marketingVO.setName(arr.getString(1));
            marketingVO.setBegDate(arr.getString(2));
            marketingVO.setEndDate(arr.getString(3));
            marketingVO.setPutQty(arr.getString(4));
            marketingVO.setGetQty(arr.getString(5));
            marketingVO.setGetRatio(arr.getString(6));
            marketingVO.setVerifyerQty(arr.getString(7));
            marketingVO.setVerifyerRatio(arr.getString(8));
            marketingVO.setSaleroom(arr.getString(9));
            marketingVO.setCreateTime(arr.getString(10));
            data.add(marketingVO);

        }

        return data;
    }

    private MultiValueMap Data(int y) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");//ID
        columns.add("ACTIVITY_NAME");// 活动名称
        columns.add("DATE_BEGIN");//开始日期
        columns.add("DATE_END");//结束日期
        columns.add("PUT_QTY");//投放人数
        columns.add("GET_QTY");//领取人数
        columns.add("GET_RATIO");//领取率
        columns.add("VERIFYER_QTY");//核销人数
        columns.add("VERIFYER_RATIO");//核销率
        columns.add("TOT_AMT_ACTUAL");//销售额
        columns.add("CREATIONDATE");//创建时间


        params.put("columns", columns);
        params.put("table", "L_MARKETINGACTIVITIES");//表名

        //设置查询规则（从第几条查询，每页显示多少条数）
        params.put("range", RANGE);
        params.put("count", "true");
        int r = Integer.valueOf(RANGE);
        params.put("start", String.valueOf(y * r));

        //设置排序规则
        HashMap orderby = new HashMap();
        orderby.put("column", "ID");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        return DATA("Query", params, account, password);
    }

    /**
     * 执行方法，向ERP传参
     *
     * @param args
     */
    public static void main(String[] args) {
        QueryMarketingVOData q = new QueryMarketingVOData("39.104.73.78:5321", "nea@burgeon.com.cn", "burgeon5321");
        try {
            System.err.println(q.getData(0));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}